package ControllerLayer;

import ModelLayer.*;

import java.util.ArrayList;
import java.util.Calendar;

/**
 * This is a controller class for the rental use case.
 * 
 * @author Group 4, dm82
 * @version 07-12-12
 */
public class RentalCtr
{
    private static RentalCtr    instance;
    private AddressCtr          addressCtr;
    private ToolCtr             toolCtr;
    private RentalContainer     rentalContainer;
    
    private static long                idCount;
    /**
     * Constructor for objects of class RentalCtr
     */
    private RentalCtr()
    {
        addressCtr = AddressCtr.getInstance();
        toolCtr = ToolCtr.getInstance();
        rentalContainer = RentalContainer.getInstance();
        idCount = 1;
    }
    
    /**
     * This method returns an instance of this class.
     * 
     * @return  An instance of RentalCtr.
     */
    public static RentalCtr getInstance()
    {
        if(instance == (null))
        {
            instance = new RentalCtr();
        }
        return instance;
    }
    
    /**
     * This method returns a tool.
     * 
     * @param   descriptionID   The id of the tool description. As a long.
     * @param   serialNumber    The serialnumber of the tool. As a long.
     * 
     * @return  The tool. As a Tool.
     */
    public Tool getTool(long descriptionID, long serialNumber)
    {
        return toolCtr.getTool(descriptionID,serialNumber);
    }
    
    /**
     * This method returns a ToolDescription.
     * 
     * @param   descriptionID   The id of the description.
     */
    public ToolDescription getToolDescription(long descriptionID)
    {
        return toolCtr.getToolDescription(descriptionID);
    }
    
    /**
     * This method returns a Person.
     * 
     * @param   id  The id of the Person. as a long.
     * 
     * @return  A Person.
     */
    public Person getPerson(long id)
    {
        return addressCtr.getPerson(id);
    }
    
    /**
     * This method creates and adds a rental agreement.
     * 
     * @param   customer    A customer who makes the rental.
     * @param   employee    The employee who facilitates the renting.
     * @param   tool        The tool that is being rented.
     * @param   startDate   The date the rental is to start.
     * @param   endDate     The date the rental is to end.
     */
    public boolean createRental(Customer customer, Employee employee, ToolDescription toolDescription, Calendar toStartDate, Calendar toEndDate, Calendar reservationDate, int status)
    {
    	Calendar tsd = toStartDate;
    	Calendar ted = toEndDate;
        if(rentalContainer.checkToolAvailability(toolDescription, toStartDate, toEndDate)){
        if(true){
            rentalContainer.addRental(new Rental(idCount,customer,employee,toolDescription,tsd,ted, reservationDate, status));
            idCount++;
            return true;
        }
        }
        return false;
    }
    
    /**
     * This method returns a list of tool descriptions
     * 
     * @return  a list of tool descriptions.
     */
    public ArrayList<ToolDescription> getToolDescriptionList()
    {
        return toolCtr.getToolDescriptionList();
    }
    
    /**
     * This method returns a list of all customers
     * 
     * @return  A list of customers.
     */
    public ArrayList<Customer> getCustomerList()
    {
        return addressCtr.getCustomerList();
    }
    
    public ArrayList<Rental> getCustomersRentals(Customer customer){
        return rentalContainer.getCustomerRentalList(customer);
    }
    
    /**
     * This method returns a list of all rentals
     * 
     * @return  The rental list
     */
    public ArrayList<Rental> getRentalList()
    {
        return rentalContainer.getRentalList();
    }
       
    /**
     * This method rents out a tool to a rental.
     * 
     * @param   id          The id of the rental.
     * @param   startDate   The current date of rental.
     * @param   tool        The tool that is being rentet.
     */
    public void rentRental(long id, Calendar startDate, Tool tool)
    {
        rentalContainer.rentRental(id, startDate, tool);
    }
    
    public ArrayList<String> getListOfRentals()
    {
    	return rentalContainer.getListOfRentals();
    }
    /**
     * This method returns a renal.
     * 
     * @param   id  The id of the rental.
     * 
     * @return  The rental.
     */
    public void setStatus(long id, int status){
    	rentalContainer.setStatus(id,  status);
    }
    public Rental getRental(long id)
    {
        return rentalContainer.getRental(id);
    }
}
